Learn to Code HTML & CSS 08
Lesson 8: Creating Lists
Contents
- How to create unordered, ordered, and description lists
- How to properly nest lists inside of other lists
- How to change the list item marker style and position
- How to use a background image instead of a list item marker
- How to horizontally display or float lists
When we want to use a list on a website, HTML provides three different types to choose from: unordered, ordered, and description lists. Choosing which type of list to use—or whether to use a list at all—comes down to the content and the most semantically appropriate option for displaying that content.
In addition to the three different types of lists available within HTML, there are multiple ways to style these lists with CSS. For example, we can choose what type of marker to use on a list. The marker could be square, round, numeric, alphabetical, or perhaps nonexistent. Also, we can decide if a list should be displayed vertically or horizontally. All of these choices play significant roles in the styling of our web pages.
Unordered Lists
An unordered list is simply a list of related items whose order does not matter. Creating an unordered list in HTML is accomplished using the unordered list block-level element, <ul
>. Each item within an unordered list is individually marked up using the list item element, <li
>.
By default, most browsers add a vertical margin and left padding to the <ul
> element and precede each <li
> element with a solid dot. This solid dot is called the list item marker, and it can be changed using CSS.
|
|
Ordered Lists
The ordered list element, <ol
>, works very much like the unordered list element; individual list items are created in the same manner. The main difference between an ordered list and an unordered list is that with an ordered list, the order in which items are presented is important. Because the order matters, instead of using a dot as the default list item marker, an ordered list uses numbers.
|
|
Ordered lists also have unique attributes available to them including start and reversed.
Start Attribute
The start attribute defines the number from which an ordered list should start. By default, ordered lists start at 1. However, there may be cases where a list should start at 30 or another number. When we use the start attribute on the <ol
> element, we can identify exactly which number an ordered list should begin counting from. The start attribute accepts only integer values, even though ordered lists may use different numbering systems, such as roman numerals.
|
|
Reversed Attribute
The reversed attribute, when used on the <ol
> element, allows a list to appear in reverse order. An ordered list of five items numbered 1 to 5 may be reversed and ordered from 5 to 1. The reversed attribute is a Boolean attribute, and as such it doesn’t accept any value.
|
|
Value Attribute
The value attribute may be used on an individual <li
> element within an ordered list to change its value within the list. The number of any list item appearing below a list item with a value attribute will be recalculated accordingly.
|
|
Description Lists
Another type of list seen online (but not as often as unordered or ordered lists) is the description list. Description lists are used to outline multiple terms and their descriptions, as in a glossary, for example.
Creating a description list in HTML is accomplished using the description list block-level element, <dl
>. Instead of using a <li
> element to mark up list items, the description list requires two block-level elements: the description term element, <dt
>, and the description element, <dd
>.
|
|
Nesting Lists
One feature that makes lists extremely powerful is their ability to be nested. Every list may be placed within another list; they can be nested continually. But the potential to nest lists indefinitely doesn’t provide free rein to do so. Lists should still be reserved specifically for where they hold the most semantic value.
One trick with nesting lists is to know where to begin and end each list and list item. Speaking specifically about unordered and ordered lists, as that is where most nesting will occur, the only element that may reside directly within the <ul
> and <ol
> elements is the <li
> element. To repeat, the only element we can place as a direct child of the <ul
> and <ol
> elements is the <li
> element. That said, once inside the <li
> element, the standard set of elements may be added, including any <ul
> or <ol
> elements.
|
|
List Item Styling
Unordered and ordered lists use list item markers by default. For unordered lists these are typically solid dots, while ordered lists typically use numbers. With CSS the style and position of these list item markers may be adjusted.
List Style Type Property
The list-style-type property is used to set the content of a list item marker. The available values range from squares and decimal numbers all the way to Armenian numbering, and the style may be placed on either the <ul
>, <ol
>, or <li
> elements within CSS.
Any list-style-type property value can be added to either unordered or ordered lists. With this in mind, it is possible to use a numeric list item marker on an unordered list and a nonnumeric marker on an ordered list.
Using an Image as a List Item Marker
There may come a time when the default list-style-type property values are not enough, and we want to customize our own list item marker. Doing so is most commonly accomplished by placing a background image on each <li
> element within a list.
The process includes removing any default list-style-type property value and adding a background image and padding to the <li
> element.
In detail, the list-style-type property value of none will remove existing list item markers. The background property will identify a background image, along with its position and repeat value, if necessary. And the padding property will provide space to the left of the text for the background image.
|
|
List Style Position Property
By default the list item marker is to the left of the content within the <li
> element. This list style positioning is described as outside, meaning all of the content will appear directly to the right, outside of the list item marker. Using the list-style-position property, we can change the default value of outside to inside or inherit.
Shorthand List Style Property
The list style properties discussed thus far, list-style-type and list-style-position, can be combined into one shorthand list-style property value. When using the list-style property, we can use one or all list style property values at a time.
Horizontally Displaying List
Occasionally we may want to display lists horizontally rather than vertically. Perhaps we want to divide a list into multiple columns, to build a navigational list, or to put a few list items in a single row. Depending on the content and desired appearance, there are a few different ways to display lists as a single line, such as by making the display property value of <li
> elements inline or inline-block or by floating them.
Displaying List
The quickest way to display a list on a single line is to give the <li
> elements a display property value of inline or inline-block. Doing so places all the <li
> elements within a single line, with a single space between each list item. When changing the display property value to inline or inline-block, the list item marker, be it a bullet, number, or other style, is removed.
More often than not, we’ll use the inline-block property value rather than the inline property value. The inline-block property value allows us to easily add vertical margins and other spacing to the <li
> elements, whereas the inline property value does not.
|
|
Floating List
Changing the display property value to inline or inline-block is quick; however, it removes the list item marker. If the list item marker is needed, floating each <li
> element is a better option than changing the display property.
Setting all <li
> elements’ float property to left will horizontally align all <li
> elements directly next to each other without any space between them. When we float each <li
> element, the list item marker is displayed by default and will actually sit on top of the <li
> element next to it. To prevent the list item marker from being displayed on top of other <li
> elements, a horizontal margin or padding should be added.
|
|
As when floating any element, this breaks the flow of the page. We must remember to clear our floats—most commonly with the clearfix technique—and return the page back to its normal flow.
Navigational List Example
We’ll often develop, and find, navigation menus using unordered lists. These lists are commonly laid out as horizontal lists, using either of the two techniques previously mentioned. Here is an example of a horizontal navigation menu marked up using an unordered list with <li
> elements displayed as inline-block elements.
|
|
|
|
Summary
Lists are used quite commonly in HTML, often in places that might not be obvious or apparent. The key is to use them as semantically as possible and to leverage them where they best fit.
Let’s recap. Within this lesson we covered the following:
- How to create unordered, ordered, and description lists
- How to properly nest lists inside of other lists
- How to change the list item marker style and position
- How to use a background image instead of a list item marker
- How to horizontally display or float lists